package com.easyn.CamLinePro;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.format.DateFormat;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import com.easyn.command.TimeInfo;

public class DateTimePicker extends FrameLayout {
	private String[] week = { getResources().getString(R.string.Sunday), getResources().getString(R.string.monday),getResources().getString(R.string.Tuesday),getResources().getString(R.string.Wednesday), getResources().getString(R.string.Thursday),getResources().getString(R.string.Friday),getResources().getString(R.string.Saturday)};
	private final NumberPicker mDateSpinner;
	private final NumberPicker mHourSpinner;
	private final NumberPicker mMinuteSpinner;
	private OnDateTimeChangedListener mOnDateTimeChangedListener;
	private TimeInfo mInfo;

	@SuppressLint("InlinedApi")
	public DateTimePicker(Context context) {
		super(context);
 
		/**
		 * 加载布局
		 */
		inflate(context, R.layout.record_set_number_picker, this);
		mInfo = new TimeInfo(getResources().getString(R.string.monday), 0, 0);
		/**
		 * 初始化控件
		 */
		mDateSpinner = (NumberPicker) this.findViewById(R.id.np_date);

		mDateSpinner.setDisplayedValues(week);
		mDateSpinner.setMinValue(0);
		mDateSpinner.setMaxValue(week.length - 1);
		mDateSpinner.setValue(1);
		mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);

		mHourSpinner = (NumberPicker) this.findViewById(R.id.np_hour);
		mHourSpinner.setMaxValue(23);
		mHourSpinner.setMinValue(0);
		mHourSpinner.setValue(0);
		mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);

		mMinuteSpinner = (NumberPicker) this.findViewById(R.id.np_minute);
		String[] mimute = { "0", "30" };
		mMinuteSpinner.setMaxValue(1);
		mMinuteSpinner.setMinValue(0);
		mMinuteSpinner.setDisplayedValues(mimute);
		mMinuteSpinner.setValue(0);
		mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
		mMinuteSpinner
				.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
	}

	/**
	 * 
	 * 控件监听器
	 */
	private NumberPicker.OnValueChangeListener mOnDateChangedListener = new OnValueChangeListener() {

		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			mInfo.week = week[newVal];
			/**
			 * 给接口传值
			 */
			onDateTimeChanged();
		}
	};

	private NumberPicker.OnValueChangeListener mOnHourChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			mInfo.hour = mHourSpinner.getValue();
			onDateTimeChanged();
		}
	};

	private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			int temp = mMinuteSpinner.getValue();
			if (temp == 1) {
				mInfo.mimute = 30;
			} else if (temp == 0) {
				mInfo.mimute = 0;
			}
			onDateTimeChanged();
		}
	};

 
	/*
	 * 接口回调 参数是当前的View 年月日小时分钟
	 */
	public interface OnDateTimeChangedListener {
		void onDateTimeChanged(DateTimePicker view, TimeInfo info);
	}

	/*
	 * 对外的公开方法
	 */
	public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
		mOnDateTimeChangedListener = callback;
	}

	private void onDateTimeChanged() {
		if (mOnDateTimeChangedListener != null) {
			mOnDateTimeChangedListener.onDateTimeChanged(this, mInfo);
		}
	}
}